package  com.example.myhttp.mvvm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.JsonObject
import com.yang.http.HttpHandler
import com.yang.http.bean.IBaseResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch


/**
 *
 * @功能描述:
 * @创建时间:     2021/8/16
 * @创建者:       阳
 * @修改时间:     2021/8/16
 * @修改者:       阳
 */
open class HttpViewModel : ViewModel() {

    private lateinit var mCoroutineScope: CoroutineScope

    val loading: MutableLiveData<Boolean> = MutableLiveData<Boolean>()
    fun launch(block: suspend CoroutineScope.() -> Unit) = viewModelScope.launch { block() }

    fun launchDialog(block: suspend CoroutineScope.() -> Unit) = viewModelScope.launch {
        loading.value = true
        block()
        loading.value = false
    }

    fun launchIO(block: suspend CoroutineScope.() -> Unit) =
        viewModelScope.launch(Dispatchers.IO) { block() }


    /**
     * 所有网络请求都在 mCoroutineScope 域中启动协程，当页面销毁时会自动取消
     */
    fun <T> launchHttp(
        block: suspend CoroutineScope.() -> IBaseResponse<T?>?,
        onResult: ((t: T) -> Unit),
        onFailed: ((code: Int, message: String?) -> Unit)? = null,
        onComplete: (() -> Unit)? = null
    ) {
        initCoroutineScope()
        mCoroutineScope.launch {
            try {
                HttpHandler.handleResult(block(), onResult, onFailed)
            } catch (e: Exception) {
                onFailed?.let {
                    HttpHandler.handleException(e, it)
                }
            } finally {
                onComplete?.invoke()
            }
        }
    }


    /**
     * 所有网络请求都在 mCoroutineScope 域中启动协程，当页面销毁时会自动取消
     */
    fun launchHttpString(
        block: suspend CoroutineScope.() -> Any?,
        onResult: ((t: Any) -> Unit),
        onFailed: ((code: Int, message: String?) -> Unit)? = null,
        onComplete: (() -> Unit)? = null
    ) {
        initCoroutineScope()
        mCoroutineScope.launch {
            try {
                HttpHandler.handleResultString(block(), onResult, onFailed)
            } catch (e: Exception) {
                onFailed?.let { HttpHandler.handleException(e, it) }
            } finally {
                onComplete?.invoke()
            }
        }
    }


    /**
     * 所有网络请求都在 mCoroutineScope 域中启动协程，当页面销毁时会自动取消
     */
    fun launchHttpJSONObject(
        block: suspend CoroutineScope.() -> JsonObject?,
        onResult: ((t: JsonObject) -> Unit),
        onFailed: ((code: Int, msg: String?) -> Unit)? = null,
        onComplete: (() -> Unit)? = null
    ) {
        initCoroutineScope()
        mCoroutineScope.launch {
            try {
                HttpHandler.handleResultString(block(), onResult, onFailed)
            } catch (e: Exception) {
                onFailed?.let { HttpHandler.handleException(e, it) }
            } finally {
                onComplete?.invoke()
            }
        }
    }


    private fun initCoroutineScope() {
        if (!this::mCoroutineScope.isInitialized) {
            mCoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
        }
    }
}